package com.sagereal.wtc.factorymode.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.sagereal.wtc.factorymode.R;

public class LcdActivity extends BaseActivity {
    private Button mTestPass,mTestFail,mTestBtn;
    private View mRootView;
    private boolean mIsRed = false,mIsGreen = false,mIsBlue = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置初始全屏模式
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 隐藏刘海
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
        getWindow().setAttributes(params);

        setContentView(R.layout.activity_lcd);
        mTestPass = findViewById(R.id.test_pass);
        mTestFail = findViewById(R.id.test_fail);
        mTestBtn = findViewById(R.id.test_btn);
        mRootView = findViewById(android.R.id.content);
        setTitle(getString(R.string.title_lcd_test));
        mTestPass.setVisibility(View.INVISIBLE);
        mTestFail.setVisibility(View.INVISIBLE);
        initView();
    }

    private void initView() {
        mTestBtn.setOnClickListener(v -> {
            mTestBtn.setEnabled(false);
            mTestPass.setEnabled(false);
            mTestFail.setEnabled(false);
            mTestPass.setVisibility(View.INVISIBLE);
            mTestFail.setVisibility(View.INVISIBLE);
            mTestBtn.setVisibility(View.INVISIBLE);
            // 设置全屏模式
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

            mIsRed = !mIsRed;
            if (mIsRed) {
                mRootView.setBackgroundColor(getResources().getColor(R.color.Red));
            }
        });

        mRootView.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (mIsRed) {
                    mIsGreen = !mIsGreen;
                    mIsRed = !mIsRed;
                    if (mIsGreen) {
                        mRootView.setBackgroundColor(getResources().getColor(R.color.Green));
                    }
                } else if (mIsGreen) {
                    mIsBlue = !mIsBlue;
                    mIsGreen = !mIsGreen;
                    if (mIsBlue) {
                        mRootView.setBackgroundColor(getResources().getColor(R.color.Blue));
                    }
                } else if (mIsBlue) {
                    mIsBlue = !mIsBlue;
                    mRootView.setBackgroundColor(getResources().getColor(R.color.transparent));
                    applySavedLanguage();
                    mTestBtn.setText(R.string.retest);
                    mTestBtn.setVisibility(View.VISIBLE);
                    mTestPass.setVisibility(View.VISIBLE);
                    mTestFail.setVisibility(View.VISIBLE);
                    mTestBtn.setEnabled(true);
                    mTestPass.setEnabled(true);
                    mTestFail.setEnabled(true);
                }
            }
            return true;
        });

        // 测试通过
        mTestPass.setOnClickListener(v -> {
            mTestBtn.setEnabled(false);
            mTestFail.setEnabled(false);
            Intent intent = new Intent();
            intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1));
            intent.putExtra("status", "pass");
            intent.putExtra("check", getIntent().getStringExtra("check"));
            setResult(RESULT_OK, intent);
            finish();
        });

        // 测试失败
        mTestFail.setOnClickListener(v -> {
            mTestBtn.setEnabled(false);
            mTestPass.setEnabled(false);
            Intent intent = new Intent();
            intent.putExtra("ItemId", getIntent().getIntExtra("ItemId", -1));
            intent.putExtra("status", "fail");
            intent.putExtra("check", getIntent().getStringExtra("check"));
            setResult(RESULT_OK, intent);
            finish();
        });
    }
}
